package com.cinema.dao.impl;

import com.cinema.dao.UserDao;
import com.cinema.db.DatabaseManager;
import com.cinema.entity.Role;
import com.cinema.entity.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户数据访问实现类
 */
public class UserDaoImpl implements UserDao {
    
    private final DatabaseManager dbManager = DatabaseManager.getInstance();
    
    @Override
    public void addUser(User user) {
        String sql = "INSERT INTO users (id, username, role, superior_id) VALUES (?, ?, ?, ?)";
        try {
            dbManager.executeUpdate(sql, user.getId(), user.getUsername(), 
                                  user.getRole().name(), user.getSuperiorId());
        } catch (SQLException e) {
            throw new RuntimeException("添加用户失败", e);
        }
    }
    
    @Override
    public User getUserById(String id) {
        String sql = "SELECT id, username, role, superior_id FROM users WHERE id = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, id)) {
            if (rs.next()) {
                return mapResultSetToUser(rs);
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("查询用户失败", e);
        }
    }
    
    @Override
    public User getUserByUsername(String username) {
        String sql = "SELECT id, username, role, superior_id FROM users WHERE username = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, username)) {
            if (rs.next()) {
                return mapResultSetToUser(rs);
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException("查询用户失败", e);
        }
    }
    
    @Override
    public List<User> getAllUsers() {
        String sql = "SELECT id, username, role, superior_id FROM users ORDER BY id";
        List<User> users = new ArrayList<>();
        try (ResultSet rs = dbManager.executeQuery(sql)) {
            while (rs.next()) {
                users.add(mapResultSetToUser(rs));
            }
            return users;
        } catch (SQLException e) {
            throw new RuntimeException("查询所有用户失败", e);
        }
    }
    
    @Override
    public void updateUser(User user) {
        String sql = "UPDATE users SET username = ?, role = ?, superior_id = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?";
        try {
            int rows = dbManager.executeUpdate(sql, user.getUsername(), 
                                             user.getRole().name(), user.getSuperiorId(), user.getId());
            if (rows == 0) {
                throw new RuntimeException("用户不存在: " + user.getId());
            }
        } catch (SQLException e) {
            throw new RuntimeException("更新用户失败", e);
        }
    }
    
    @Override
    public void deleteUser(String id) {
        String sql = "DELETE FROM users WHERE id = ?";
        try {
            int rows = dbManager.executeUpdate(sql, id);
            if (rows == 0) {
                throw new RuntimeException("用户不存在: " + id);
            }
        } catch (SQLException e) {
            throw new RuntimeException("删除用户失败", e);
        }
    }
    
    @Override
    public boolean userExists(String id) {
        String sql = "SELECT COUNT(*) FROM users WHERE id = ?";
        try (ResultSet rs = dbManager.executeQuery(sql, id)) {
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException("检查用户是否存在失败", e);
        }
    }
    
    private User mapResultSetToUser(ResultSet rs) throws SQLException {
        String id = rs.getString("id");
        String username = rs.getString("username");
        Role role = Role.valueOf(rs.getString("role"));
        String superiorId = rs.getString("superior_id");
        
        return new User(id, username, role, superiorId);
    }
} 